package com.hsy.bibc.modules.exchange;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.code.zxing.MipcaActivityCapture;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/7.
 */
public class ExChangeActivity extends CommonBaseActivityWithSimpleActionBar {

    private LinearLayout code;
    private TextView finish;
    private EditText code_edit;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
//            AfterExchangeCodeGsonData.Result result = DPGsonUtil.fromJson(response, AfterExchangeCodeGsonData.Result.class);
            startActivity(AfterExchangeCodeActivity.class, code_edit.getText().toString());
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange);
        setTitle(R.string.title_dh);

        code = F(R.id.code_line);
        finish = F(R.id.finish);
        code_edit = F(R.id.code_edit);

        code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ExChangeActivity.this, MipcaActivityCapture.class);
                intent.putExtra(Consts.Keys.FROM_WHERE, "2");
                startActivity(intent);
            }
        });

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isEmpty(code_edit.getText().toString())) {
                    showToast(R.string.msg_forget_code);
                    return;
                }
                CommonUtils.showProgressDialog(ExChangeActivity.this);
                Map<String, String> params = new HashMap<>();
                params.put("token", CommonUtils.getToken(ExChangeActivity.this));
                params.put("code", code_edit.getText().toString().trim());
                HttpUtil.httpPostWithParams(ExChangeActivity.this, 0, Consts.Apis.POST_COMMIT_DH, params, callback);

            }
        });
    }
}
